package kr.hkit.androidcomplete02.ch19;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class RunOnUiThread extends Activity {
	int mainValue;
	int backValue;
	TextView mainTv;
	TextView backTv;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.ch19_threadex);

		mainTv = (TextView) findViewById(R.id.ch19_threadex_mainvalue);
		backTv = (TextView) findViewById(R.id.ch19_threadex_backvalue);

		BackThread thread = new BackThread();
		thread.setDaemon(true);
		thread.start();
	}

	public void mOnClick(View v) {
		if (v.getId() == R.id.ch19_threadex_increase) {
			mainValue++;
			mainTv.setText("MainValue : " + mainValue);
		}
	}
		
	class BackThread extends Thread {
		public void run() {
			while (true) {
				backValue++;
				runOnUiThread(new Runnable() {
					public void run() {
						backTv.setText("BackValue : " +backValue);
					}
				});
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}